$(function () {
    //工具栏快速搜索框事件
    $("#offer-search").searchbox({
        searcher: function () {
            //获取快速搜索的类型
            var searchName = $('#offer-search').searchbox('getName');
            //获取搜索框的值
            var searchVal = $('#offer-search').searchbox('getValue');
            if (searchName == 'theme') {
                clearDataGrid();
                $('#zjk-offer-dg').datagrid({
                    queryParams: null
                });
                $('#zjk-offer-dg').datagrid({
                    url: 'zjk-showOfferList',
                    method: 'post',
                    queryParams: {
                        theme: searchVal
                    }
                });

                $('#zjk-offer-dg').datagrid('reload');
            } else if (searchName == 'ofType') {
                clearDataGrid();
                $('#zjk-offer-dg').datagrid({
                    queryParams: null
                });
                $('#zjk-offer-dg').datagrid({
                    url: 'zjk-showOfferList',
                    method: 'post',
                    queryParams: {
                        ofType: searchVal
                    }
                });

                $('#zjk-offer-dg').datagrid('reload');

            }


        }

    });

    //点击工具栏的“查询所有”按钮，查询所有销售计划
    $("#zjk-search-all-offer-btn").linkbutton({
        onClick: function () {
            $('#zjk-offer-dg').datagrid({
                queryParams: null
            });
            $('#zjk-offer-dg').datagrid({
                url: 'zjk-showOfferList',
                method: 'post',
                queryParams: {
                    stage: null
                },
            });
            $('#zjk-offer-dg').datagrid('reload');
            $("#offer-search").searchbox('setValue',null);
        }
    });

    //点击计划主表中一行，下方从表显示对应计划明细
    $("#zjk-offer-dg").datagrid({
        onClickCell: function (index) {
            var rows = $('#zjk-offer-dg').datagrid('getRows');
            var id = rows[index].planId;
            $('#zjk-offer-edtalis-dg').datagrid({
                url: 'zjk-getOfferDetailsVOList',
                queryParams: {
                    ofId: rows[index].ofId
                },
                method: 'post'
            });
        }
    });
    //工具栏“新建”按钮事件
    $("#zjk-add-offer-btn").linkbutton({
        onClick: function () {

            $('#zjk-offer-add-dialog').dialog({
                onLoad: function () {
                    $("#zjk-add-offer-clientId").combobox({
                        url: 'saling/getClientInfoList',
                        method: 'post',
                        valueField: 'cid',
                        textField: 'cname',
                        panelHeight: 'auto',
                        onSelect: function (record) {
                            var url = "saling/loadContactInformation";
                            $("#zjk-add-offer-contactId").combobox({
                                url: url,
                                method: 'post',
                                panelHeight: 'auto',
                                queryParams: {'cid': record.cid},
                                valueField: 'contactid',
                                textField: 'contactName'
                            });

                        }
                    });
                    $("#zjk-add-offer-userId").combobox({
                        url: 'saling/getUserInfoList',
                        method: 'post',
                        panelHeight: 'auto',
                        valueField: 'uId',
                        textField: 'uname'
                    });

                    $("#zjk-add-offer-chanceId").combobox({
                        url:'getSaleChanceName',
                        method:'post',
                        panelHeight:'auto',
                        valueField:'chId',
                        textField:'chanceTheme'

                    });

                }
            });
            $('#zjk-offer-add-dialog').dialog('open');
        },




    });
    //工具栏“新建”关闭按钮事件
    $("#zjk-offer-add-bb-close").linkbutton({
        onClick:function () {
            $('#zjk-offer-add-dialog').dialog('close');

        }
    })
    //工具栏“新建”保存按钮事件
    $("#zjk-offer-add-bb-save").linkbutton({
        onClick: function () {
            //提交表单事件
            $('#zjk-add-offer-f').form({
                onSubmit: function (parmps) {
                    //表单验证
                    return $(this).form('validate');
                },
                success: function (r) {
                    if (r!=null) {
                        $.messager.alert("提示", "恭喜，添加成功！您可在操作栏再次编辑计划及计划明细。");

                        //刷新展示列表
                        $('#zjk-offer-dg').datagrid('reload');
                        //关闭窗口
                        $('#zjk-offer-add-dialog').dialog('close');
                        //清空表单
                        $('#zjk-add-offer-f').form('clear');


                    } else {
                        $.messager.alert("错误", r.messager);
                    }

                }
            }).submit();


        }
    })

    //工具栏删除按钮事件
    $("#zjk-delete-offer-btn").linkbutton({
        onClick: function () {
            var rows = $('#zjk-offer-dg').datagrid('getChecked');
            if (rows.length == 0) {
                $.messager.alert("提示", "请先选择要删除的行！");
            } else {
                $.messager.confirm("询问", "您确定要删除吗？", function (v) {
                    if (v) {
                        for (var i = 0; i < rows.length; i++) {
                            if (rows[i]) {
                                var id = rows[i].ofId;
                                $.post('zjk-deleteOfferById', {ofId: id}, function (r) {
                                    if (r.success) {
                                        $.messager.alert("提示", "恭喜，删除成功！");

                                        $('#zjk-offer-dg').datagrid('reload');
                                    } else {
                                        $.messager.alert("提示", r.messager);
                                    }
                                });
                            }
                        }
                    }
                })
            }
        }
    });

    //操作栏事件处理
    $("#zjk-offer-dg").datagrid({
        onLoadSuccess:function (data) {
            $("a[name='offer-details']").linkbutton({
                width:40,iconCls:'icon-my_detail',plain:true
            });
            //操作栏删除
            $("a[name='offer-delete']").linkbutton({
                width: 40, iconCls: 'icon-my_delete', plain: true,
                onClick: function () {
                    $('#zjk-offer-dg').datagrid({
                        onClickCell: function (index) {
                            var rows = $('#zjk-offer-dg').datagrid('getRows');
                            var ofId = rows[index].ofId;
                            $.post('zjk-deleteOfferById', {ofId: ofId}, function (r) {
                                if (r.success) {
                                    $.messager.alert("提示", "恭喜，删除成功！");
                                    $('#zjk-offer-dg').datagrid('reload');
                                } else {
                                    $.messager.alert("提示", r.message);
                                }
                            });
                            $('#zjk-offer-dg').datagrid({
                                onClickCell: function (index) {
                                }
                            });
                        }

                    });


                }
            });
            //编辑
            $("a[name='offer-edit']").linkbutton({
                width:40,iconCls:'icon-my_edit',plain:true,
                onClick: function () {
                    $('#zjk-offer-dg').datagrid({
                        onClickCell:function (index) {
                            var rows = $('#zjk-offer-dg').datagrid('getRows');
                            var row = rows[index];// 根据index获得其中一行。
                            if (row) {
                                $('#zjk-offer-edit-dialog').dialog({
                                    onLoad: function () {
                                        $("#zjk-edit-offer-f").form('load', row);
                                        $("#zjk-edit-offer-clientId").combobox({
                                            url: 'saling/getClientInfoList',
                                            method: 'post',
                                            valueField: 'cid',
                                            textField: 'cname',
                                            panelHeight: 'auto',
                                            editable:false,
                                            readonly:true,
                                            onSelect: function (record) {
                                                var url = "saling/loadContactInformation";
                                                $("#zjk-edit-offer-contactId").combobox({
                                                    url: url,
                                                    method: 'post',
                                                    panelHeight: 'auto',
                                                    queryParams: {'cid': record.cid},
                                                    valueField: 'contactid',
                                                    textField: 'contactName',
                                                });

                                            }
                                        });
                                        $("#zjk-edit-offer-userId").combobox({
                                            url: 'saling/getUserInfoList',
                                            method: 'post',
                                            panelHeight: 'auto',
                                            valueField: 'uId',
                                            textField: 'uname',
                                            editable:false,
                                            readonly:true
                                        });

                                        $("#zjk-edit-offer-chanceId").combobox({
                                            url:'getSaleChanceName',
                                            method:'post',
                                            panelHeight:'auto',
                                            valueField:'chId',
                                            textField:'chanceTheme',
                                            hasDownArrow: false,
                                            editable:false,
                                            readonly:true

                                        });
                                        $("#zjk-edit-offer-f").form('load', row);

                                        $('#zjk-offer-detail-dg').datagrid({
                                            url: 'zjk-getOfferDetailsVOList',
                                            queryParams: {
                                                ofId: rows[index].ofId
                                            },
                                            method: 'post'
                                        });

                                    }

                                });
                                //加载完成后打开窗口
                                $('#zjk-offer-edit-dialog').dialog('open')
                            }
                        }
                    });

                }
            });

        }
    });


    //编辑窗口中的最大窗口的“关闭”按钮事件
    $("#zjk-offer-edit-dialog-bb-close").linkbutton({
        onClick:function () {
            $('#zjk-offer-edit-dialog').dialog('close');
        }
    })

    //编辑窗口中的最大窗口的“保存”按钮事件
    $("#zjk-offer-edit-dialog-bb-save").linkbutton({
        onClick:function () {
            $('#zjk-edit-offer-f').form({
                onSubmit:function (parmps) {
                    //进行验证
                    return $(this).form('validate');

                },
                success:function (r) {
                    r = eval('('+r+')');
                    if(r.success){
                        //点击关闭，则窗口关闭
                        $.messager.alert('成功', '恭喜，修改成功！');
                        $('#zjk-offer-edit-dialog').dialog('close');
                        $('#zjk-offer-dg').datagrid('reload');
                    }else {
                        $.messager.alert("错误",r.messager);
                    }
                }
            }).submit();

        }
    });
    //打开编辑中的"添加"明细窗口
    $('#zjk-offer-edtails-tools-add').linkbutton({
        onClick:function () {
            $('#add-offer-details').dialog('open');

        }
    });

    //编辑窗口的添加关闭按钮事件
    $("#zjk-offer-edtails-add-dialog-bb-close").linkbutton({onClick:function () {
            $('#add-offer-details').dialog('close');
        }})
    //编辑窗口的添加保存按钮事件
    $("#zjk-offer-edtails-add-dialog-bb-save").linkbutton({
        onClick:function () {
            $('#zjk-offer-add-detail-f').form({
                url:'add-offerDetail',
                method:'get',
                onSubmit: function () {
                    return $(this).form('validate')
                },
                success: function (data) {
                    $('#add-offer-details').dialog('close');
                    $('#zjk-offer-detail-dg').datagrid('reload');
                    $('#zjk-offer-add-detail-f').form('clear');
                    //刷新展示列表
                    $('#zjk-offer-dg').datagrid('reload');
                },
            }).submit();

        }});

    //编辑窗口中的“删除”按钮事件
    $("#zjk-offer-edtails-tools-remove").linkbutton({
        onClick: function () {
            var row = $("#zjk-offer-detail-dg").datagrid("getSelected")
            if (row != null) {
                var index = $("#zjk-offer-detail-dg").datagrid('getRowIndex', row);
                $("#zjk-offer-detail-dg").datagrid('deleteRow', index)
                rows = JSON.stringify(row)
                $.post("zjk-deletePlanDetails", {
                        rows: rows,
                        saleDetId: row.row.planDetail.saleDetId
                    },
                    function (result) {
                        if (result.success == true) {
                            $.messager.alert("提示", "删除成功")
                        } else {
                            $.messager.alert("提示", result.messager)

                        }
                    }
                )

            } else {
                $.messager.alert("提示", "请先选择要删除的行！")
            }
        }
    });




    //编辑窗口中的“修改”按钮事件
    $("#zjk-offer-edtails-tools-edit").linkbutton({
        onClick: function () {
            var row = $("#zjk-offer-detail-dg").datagrid('getSelected')
            if (row) {
                $("#edit-offer-details").dialog({
                    onLoad: function () {
                        $.post('getOneOfferDetail', {deId: row.offerDetail.deId}, function (r) {
                            if (r) {
                                $('#zjk-offer-edit-detail-pname').textbox({value: r.productionDetail.pname})
                                $('#zjk-offer-edit-detail-pId').textbox({value: r.productionDetail.pid})
                                $('#zjk-offer-edit-detail-pwunit').textbox({value: r.productionDetail.pwunit})
                                $('#zjk-offer-edit-detail-poprice').textbox({value: r.productionDetail.poprice})
                                $('#zjk-offer-edit-detail-counts').textbox({value: r.offerDetail.counts})
                                $('#zjk-offer-edit-detail-totalMoney').textbox({value: r.offerDetail.totalMoney})
                                $('#zjk-offer-edit-detail-setTime').datebox({value: r.offerDetail.setTime})
                                $('#deId').val(r.offerDetail.deId)

                            }

                        })

                    },
                });
                $("#zjk-offer-edtails-edit-dialog-bb-save").linkbutton({
                    onClick: function () {
                        $('#').form({
                            url: 'saveEditOfferDetail',
                            onSubmit: function () {
                                var isValid = $(this).form('validate');
                                return isValid;
                            },
                            success: function (data) {
                                $('#edit-offer-details').dialog('close')
                                $('#zjk-offer-detail-dg').datagrid('reload')

                            }
                        }).submit();
                    }
                })
                $("#edit-offer-details").window('open')
            } else {
                $.messager.alert('提示', "请先选中一行！！！")
            }


        }
    });



})


//添加操作栏图标
function format(value,row,index){
    var str=/*'<a name="offer-details" href="#" class="easyui-linkbutton " title="视图"></a>'+*/
        '<a name="offer-delete" href="#" class="easyui-linkbutton " title="删除"></a>'+
        '<a name="offer-edit" href="#" class="easyui-linkbutton zjk-edit-offer-btn" title="编辑"></a>';
    return str;

}
/* 清除数据表格中的数据 */
function clearDataGrid() {
    // 获取当前页的记录数
    var item = $('#zjk-offer-dg').datagrid('getRows');
    if (item != null) {
        for (var i = item.length - 1; i >= 0; i--) {
            var index = $('#zjk-offer-dg').datagrid('getRowIndex', item[i]);
            $('#zjk-offer-dg').datagrid('deleteRow', index);
        }
    }

}

function formatM(value,row,index){
    if(value!=null){
        var y = String(value).indexOf(".") + 1;//获取小数点的位置
        var count = String(value).length - y;//获取小数点后的个数
        if(y > 0 && count == 1){
            return '<b style="color: #9cc8f7;">'+"￥"+value+ "0" + '</b>';
        }else if (y > 0 && count > 1){
            return '<b style="color: #9cc8f7;">'+"￥"+value+ '</b>';
        } else {
            return '<b style="color: #9cc8f7;">' + "￥" + value + ".00" + '</b>';
        }
    }else {
        return value;
    }
}